<h1>
    <?=$this->title ?>
</h1>
<?
    if (!$this->is_add) // Edit Mode
    {
?>
<div id="right_menu">
    <ul>
<?
        $i = 0;
        foreach ($this->menu as $title => $action)
        {
            $i++;
            $container = ($action == 'edit' || $action == 'delete') ? 'inner' : 'container';
?>
        <li
<?
            if (count($this->menu) == $i)
                   echo "class=\"last\"";

            if ($this->action == $action)
                echo "class=\"active\"";
?>
            >

            <a href="#"
<?
            if ($action == 'delete')
            {
?>
                onClick="if (confirm('Sure?')) {ajaxSpa({id: <?=$this->model_spa->getId() ?>,
                                                                    action: 'delete',
                                                                    container: 'inner',
                                                                    company_id: <?=$this->model_spa->getCompanyID() ?>,
                                                                    type_id: <?=$this->model_spa->getModelType()->getID() ?>}); return false;}"
<?
            }
            else
            {
?>
                onClick="ajaxSpa({id: <?=$this->model_spa->getID() ?>,
                                       container: '<?=$container ?>',
                                       action: '<?=$action;?>', no_apply: true}); return false;"
<?
            };
?>
                ><?=$title ?></a>
        </li>
<?
        }; //foreach
?>
    </ul>
</div>
<?
    }; //if (!$this->is_add)
?>
<div id="container" <? if ($this->is_add) echo "style = \"margin-top: -50px;\""; ?>>
    <div>
<?
    Principio_Library_Common::printErrorsAndMsg();
    if (!$this->is_add)
        echo "<div class=\"spa_left\">";
?>
        <div class="subtitle">
            General Settings:
        </div>
        <div class="hr"></div>
        <table class="form_table">
            <tr>
                <td class="title">Provisioning enabled:</td>
                <td class="input_field"><input type="checkbox" id="spa_provisioning_enabled" value="" checked style="width: 20px;"/></td>
            </tr>
            <tr>
                <td class="title">Time Zone:</td>
                <td class="input_field"><input type="text" id="spa_time_zone" value="Time zone" /></td>
            </tr>
            <tr>
                <td class="title">Dictionary:</td>
                <td class="input_field"><input type="text" id="spa_dictionary" value="Dictionary" /></td>
            </tr>
            <tr>
                <td class="title">IP:</td>
                <td class="input_field">
                    <select id="spa_ip">
<?
    foreach ($this->ip_not_in_use as $ip)
	{
?>
                        <option value="<?=$ip ?>" <? if ($this->model_spa->getIP() == $ip) { echo " selected"; }?>><?=$ip ?></option>
<?
    };
?>
	                </select>
                </td>
            </tr>
            <tr>
                <td class="title"> MAC:</td>
                <td class="input_field"><input type="text" id="spa_mac" value="<?=$this->model_spa->getMAC() ?>" /></td>
            </tr>
            <tr>
                <td class="title">S/N:</td>
                <td class="input_field"><input type="text" id="spa_serial" value="<?=$this->model_spa->getSerial() ?>" /></td>
            </tr>
        </table>
<?
        if (!$this->is_add)
        {
?>
        </div>
        <div class = "spa_right">
            <div class = "subtitle info">
                General Info:
            </div>
            <div class = "hr"></div>
            <table class="form_table">
                <tr>
                    <td class="title">Provisioning status:</td>
                    <td class="input_field"><?=$this->model_spa->getProvStatus() ?></td>
                </tr>
                <tr>
                    <td class="title">Last provisioned attempt:</td>
                    <td class="input_field"><?=$this->model_spa->getLastProvAttempt() ?></td>
                </tr>
                <tr>
                    <td class="title">Last successfull provisioning:</td>
                    <td class="input_field"><?=$this->model_spa->getLastProv() ?></td>
                </tr>
                <tr>
                    <td class="title">Last taken firmware:</td>
                    <td class="input_field"><?=$this->model_spa->getLastFirmware() ?></td>
                </tr>
                <tr>
                    <td class="title">Last provisioning message:</td>
                    <td class="input_field"><?=$this->model_spa->getLastProvMsg() ?></td>
                </tr>
            </table>
        </div>
        <div class = "clear"></div>
<?
        };
?>
        <br><br>
<?
    for ($i = 0; $i < $this->model_spa->getModelType()->getExts(); $i++)
    {
?>
    	<div class="subtitle">
            Ext <?=($i + 1)?>:
	    </div>
	    <div class="hr"></div>
        <table class="form_table">
            <tr>
                <td class="title">Company:</td>
                <td class="input_field">
                    <select id="company_id_<?=$i ?>" onchange="event_onChange_CompanyID(<?=$i ?>)">
                        <option value="-1">Empty</option>
<?
        foreach ($this->companies as $company)
        {
?>
                        <option value="<?=$company->getID() ?>" <? if (($this->model_spa->getModelAccount($i)->getCompanyID() == $company->getID()) || (($this->model_spa->getModelAccount($i)->getCompanyID() == -1) && ($this->model_spa->getCompanyID() == $company->getID()))) { echo "selected"; } ?>>
                                <?=$company->getCaption() ?>
                        </option>
<?
        };
?>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="title">Account:</td>
                <td class="input_field">
                    <select id="spa_ext_<?=$i ?>">
                        <option value="-1">Empty</option>
<?
        foreach ($this->accounts[$this->model_spa->getModelAccount($i)->getCompanyID()] as $account)
        {
?>
                        <option value="<?=$account->getID() ?>" <? if ($this->model_spa->getModelAccount($i)->getID() == $account->getID()) { echo "selected"; } ?>>
                                <?="Login: ".$account->getLogin()." (Display name: ".$account->getDisplayName()."; Short name: ".$account->getShortName() ?>)
                        </option>
<?
        };
?>
                    </select>
                </td>
            </tr>
        </table>
        <br>
<?
    };
?>
        <table border="0" width="100%" class="buttons">
            <tr>
                <td align="right">
                    <input class="save" type="submit" name="apply" value="<?=($this->is_add ? "Add device" : "Apply changes") ?>" onClick="ajaxSpa({action: '<?=($this->is_add ? "add" : "edit") ?>',
                                                                                                             <? if (!$this->is_add) echo " id: ".$this->model_spa->getID().", "; ?>
                                                                                                             container: 'inner',
                                                                                                             company_id: <?=$this->model_spa->getCompanyID() ?>,
                                                                                                             type_id: <?=$this->model_spa->getModelType()->getID() ?>,
                                                                                                             ext_count: <?=$this->model_spa->getModelType()->getExts() ?>
                                                                                                             })"/>
                    <input class="cancel" type="submit" name="reset" value="Reset" onClick="<?=($this->is_add) ? ("addItem({menu: 'spa', container: 'inner', company_id: ".$this->model_spa->getCompanyID().", type_id: ".$this->model_spa->getModelType()->getID()."});")
                                                                                                               : ("ajaxSpa({id: ".$this->model_spa->getID().", action: 'edit', container: 'inner', no_apply: true});") ?>"/>
                </td>
            </tr>
        </table>
    </div>
</div>